﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpDemo._04_泛型
{
    class LinkedListNode
    {
        //属性
        public object Value { get; set; }
        public LinkedListNode Prev { get; set; }
        public LinkedListNode Next { get; set; }

        public LinkedListNode(object Value)
        {
            this.Value = Value;
        }
    }


    class LinkedList : IEnumerable
    {
        public LinkedListNode First { get; set; }
        public LinkedListNode Last { get; set; }

        public LinkedListNode Add(object node)
        {
            LinkedListNode newNode = new LinkedListNode(node);
            //添加的是第一个节点
            if (First == null)
            {
                First = newNode;
                Last = newNode;
            }
            else
            {
                LinkedListNode cur = this.Last;
                cur.Next = newNode;
                newNode.Prev = cur;
                this.Last = newNode;
            }
            return newNode;
        }

        public IEnumerator GetEnumerator()
        {
            LinkedListNode current = First;
            while (current != null)
            {
                yield return current.Value;
                current = current.Next;
            }
        }
    }

    class NotGenericTest
    {
        public static void Test()
        {
            LinkedList Link = new LinkedList();
            //1.添加的是否执行装箱操作， 把基本类型转化为object对象
            Link.Add(2);
            Link.Add(3);

            //3.拆箱操作时 引发运行时异常
            //Link.Add("string");

            Console.WriteLine("\n非泛型链表测试");
            //2.遍历的时候执行拆箱操作， 把object对象转化为int类型
            foreach (int a in Link)
            {
                Console.WriteLine(a);
            }
        }
    }
}
